package com.syni.mdd.sjb.common.component.utils.common.utils;

import com.syni.mdd.sjb.common.component.utils.common.constants.CommonResultCode;
import com.syni.mdd.sjb.common.component.utils.common.constants.CommonResultMsg;
import com.syni.mdd.sjb.common.component.utils.common.enums.ReturnEnums;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class ResultMapHelper {

    public static Map<String, Object> result(String status, String msg, Object data) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("status", status);
        result.put("msg", msg);
        result.put("data", data);
        return result;
    }

    public static Map<String, Object> result(String status, String msg) {
        return result(status, msg, null);
    }

    /**
     * @Description: 新方式，采用枚举的方式
     * @Param returnEnums
     * @Author: cfx
     * @Date: 2019/12/15
     */
    public static Map<String, Object> result(ReturnEnums returnEnums) {
        return result(returnEnums.getCode(), returnEnums.getMsg(), null);
    }


    public static Map<String, Object> failure(String msg) {
        return result(CommonResultCode.CODE_ERROR, msg, null);
    }

    public static Map<String, Object> success(Object obj, String msg) {
        return result(CommonResultCode.CODE_NORMAL, msg, obj);
    }

    public static Map<String, Object> success(Object obj, String msg, Boolean allowNull) {
        if (allowNull && Objects.isNull(obj)) {
            return result(CommonResultCode.CODE_NORMAL, msg, new HashMap<>());
        }
        return result(CommonResultCode.CODE_NORMAL, msg, obj);
    }

    public static Map<String, Object> success(Object obj, Map<String, Object> userData, String msg) {
        Map<String, Object> resultMap = result(CommonResultCode.CODE_NORMAL, msg, obj);
        resultMap.put("userData", userData);
        return resultMap;
    }

    public static Map<String, Object> success(Object obj, Map<String, Object> userData) {
        Map<String, Object> resultMap = result(CommonResultCode.CODE_NORMAL, CommonResultMsg.HAND_SUCCESS, obj);
        resultMap.put("userData", userData);
        return resultMap;
    }

    public static Map<String, Object> success(Object obj) {
        return result(CommonResultCode.CODE_NORMAL, CommonResultMsg.HAND_SUCCESS, obj);
    }

    public static Map<String, Object> success() {
        return result(CommonResultCode.CODE_NORMAL, null, new HashMap<>());
    }


    public static boolean isSuccess(Map<String, Object> result) {
        if (result.containsKey("status")) {
            String status = result.get("status").toString();
            return StringUtils.equals(status, CommonResultCode.CODE_NORMAL);
        }
        return false;
    }

    public static Map<String, Object> nullParamsResult() {
        return result(CommonResultCode.CODE_ERROR_01, "相关参数不能为空");
    }

    public static Map<String, Object> bizError(String msg) {
        return result(CommonResultCode.CODE_ERROR_100, msg);
    }

}